////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.stock;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.StockGroup;

/**
 * @author haibo for list item named "Instant Market"
 */
public class CurrentStockNewsContentActivity extends HttpSuperClassStockBaseActivity
{
	protected static final String	TAG				= CurrentStockNewsContentActivity.class.getSimpleName();
	public static String			_content;
	public static String			_title;
	public static String			_timestamp;
//	public final static String		KEY_CONTENT		= "key.content";
//	public final static String		KEY_TITLE		= "key.title";
//	public final static String		KEY_TIMESTAMP	= "key.timestamp";
	
	// private TextView _txt;
	private WebView					_webView;
	private TextView				_newsTitle;
	private TextView				_newsTimestamp;
	private Button					_toolBtnReturn;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.stock_news_item_content);

		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "close current activity and forwarding to the previous");
				Intent intent = new Intent(getApplicationContext(), CurrentStockNewsListActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				CurrentStockNewsListActivity._isComeFromBack = true;
				SwitchForwardActivity(CurrentStockNewsListActivity.class, intent);
			}

		});

		_newsTimestamp = (TextView) findViewById(R.id.list_item_timestamp);
		_newsTitle = (TextView) findViewById(R.id.list_item_title);

		/*
		 * _txt = (TextView)findViewById(R.id.list_item_content); //
		 * _txt.setAutoLinkMask(Linkify.ALL);
		 * _txt.setAutoLinkMask(Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES);
		 */

		_webView = (WebView) findViewById(R.id.webview);
		WebSettings webSettings = _webView.getSettings();
		webSettings.setDefaultFontSize(24);
	}
	
	@Override
	protected void onResume()
	{
		if(_webView == null)
			_webView = (WebView) findViewById(R.id.webview);
		_webView.clearCache(true);
		_webView.clearView();
		_webView.clearHistory();
		
		if (appHost.screenIsLocked)
		{
			updateContent();
		}
		
		super.onResume();
	}
	
	protected void onPause()
	{
		super.onPause();
		_webView.clearSslPreferences();
		_webView.loadUrl("about:blank");
		_webView.clearHistory();
	}

	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return StockGroup.GetActivityGroupHostControl();
	}
	
	static public String iID = "", iVendor = "";
	
	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		clearContent();
		
		return HttpAppInfo.SendStockNewsContentRequest(actContext, iID, iVendor, this);
	}

	@Override
	public void H_UserClient_HttpRequest_Response_Sub(H_UserClient_HttpRequest_Result iResult)
	{
//		DismissProgressSunFlower();

		if (iResult.MStatus.equals("0") == false)
		{
			// Prompt Error
			ShowMessageDialog(iResult.MError + "(" + iResult.MStatus + ")");
			return;
		}

		TDStockLog.d(TAG, "TMP=>Size:" + iResult.contentStores.size());
		for (String[] tmp : iResult.contentStores)
		{
			TDStockLog.d(TAG, "TMP=>" + tmp[0] + "," + tmp[1] + "," + tmp[2] + "," + tmp[3]);
		}
		
		
//		if (map.containsKey("ErrorMessage") && map.get("ErrorMessage") != null)
//		{
//			_newsContent.setLength(0);
//			_newsContent.append(map.get("ErrorMessage"));
//			_timestamp = "";
//			_title = "";
//		}
//		else
//		{
			String[] result = iResult.contentStores.get(0);
			
			_timestamp = result[0].length() >= 16 ? result[0].substring(0, 16) :result[0];
			_title = result[1];
			_content = result[2];
//		}
		
		updateContent();
	}
	
	private void clearContent()
	{
		_timestamp = "";
		_title = "";
		_content = "";
		updateContent();
	}

	private void updateContent()
	{
		/* _txt.setText(Html.fromHtml( _content)); */
		/*
		 * Bundle bundle = this.getIntent().getExtras(); _content =
		 * bundle.getString(KEY_CONTENT); _title = bundle.getString(KEY_TITLE);
		 * _timestamp = bundle.getString(KEY_TIMESTAMP);
		 */
		
		if (_webView != null)
		{
			String summary = "<html><body><div>" 
							+ _content.replaceAll("[\n]", "<br>").replaceAll("&gt;", ">").replaceAll("&lt;", "<").replaceAll("&amp;", "&") 
							+ "</div></body></html>";
			
			TDStockLog.d(TAG, "content=" + summary);
			
			// _webView.loadData(summary, "text/html", "utf-8");
			_webView.getSettings().setBuiltInZoomControls(true);
			_webView.getSettings().setJavaScriptEnabled(true);
			_webView.loadDataWithBaseURL(null, summary, "text/html", "utf-8", null);
		}

		_toolBtnReturn.setText((((ITDLApplication) getApplicationContext()).getCurrentStockName()));

		if (_title != null)
		{
			// _newsTitle.setText(_title);
			_newsTitle.setText(Html.fromHtml(_title));
		}
		_newsTitle.setVisibility((_title != null && _title.length() >= 1) ? View.VISIBLE : View.INVISIBLE);

		if (_timestamp != null)
		{
			_newsTimestamp.setText(_timestamp);
		}
	}

}
